package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.WholeProcessInterUvStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.WholeProcessInterUvStatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.WholeProcessInterUvStatSearchResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatWholeProcessInter.STAT_WHOLE_PROCESS_INTER;

@Repository
public class WholeProcessInterUvStatRepositoryImpl extends JOOQBaseRepositoryPlus implements WholeProcessInterUvStatRepository{

    @Override
    protected TableImpl getTable() {
        return STAT_WHOLE_PROCESS_INTER;
    }

    @Override
    public Page<WholeProcessInterUvStatSearchResponse> list(WholeProcessInterUvStatSearchRequest searchRequest, Pageable pageable) {
        fields();
        if (Objects.nonNull(searchRequest.getChannel())){
            eq(STAT_WHOLE_PROCESS_INTER.CHANNEL,searchRequest.getChannel());
        }
        if (Objects.nonNull(searchRequest.getProId())){
            eq(STAT_WHOLE_PROCESS_INTER.PROID,searchRequest.getProId());
        }
        if (Objects.nonNull(searchRequest.getActivity_name())){
            eq(STAT_WHOLE_PROCESS_INTER.ACTIVITY_NAME,searchRequest.getActivity_name());
        }
        if (Objects.nonNull(searchRequest.getStartDate())){
            ge(STAT_WHOLE_PROCESS_INTER.TIME,searchRequest.getStartDate());
        }

        if (Objects.nonNull(searchRequest.getEndDate())){
            le(STAT_WHOLE_PROCESS_INTER.TIME,searchRequest.getEndDate());
        }

        if (Objects.isNull(searchRequest.getEndDate())&&Objects.isNull(searchRequest.getStartDate())){
            between(STAT_WHOLE_PROCESS_INTER.TIME,searchRequest.getStartDate(),searchRequest.getEndDate());
        }
        return search(pageable,record -> {
            WholeProcessInterUvStatSearchResponse response = new WholeProcessInterUvStatSearchResponse();
            response.setChannel(record.get(STAT_WHOLE_PROCESS_INTER.CHANNEL));
            response.setProid(record.get(STAT_WHOLE_PROCESS_INTER.PROID));
            response.setTime(record.get(STAT_WHOLE_PROCESS_INTER.TIME));
            response.setActivity_name(record.get(STAT_WHOLE_PROCESS_INTER.ACTIVITY_NAME));
            response.setOwnNer_call_filter_uv(record.get(STAT_WHOLE_PROCESS_INTER.OWNNER_CALL_FILTER_UV));
            response.setOwnNer_pass_filter_uv(record.get(STAT_WHOLE_PROCESS_INTER.OWNNER_PASS_FILTER_UV));
            response.setFace_pass_uv(record.get(STAT_WHOLE_PROCESS_INTER.FACE_PASS_UV));
            response.setOperator_call_uv(record.get(STAT_WHOLE_PROCESS_INTER.OPERATOR_CALL_UV));
            response.setOperator_pass_uv(record.get(STAT_WHOLE_PROCESS_INTER.OPERATOR_PASS_UV));
            response.setOwnNer_call_push_order_uv(record.get(STAT_WHOLE_PROCESS_INTER.OWNNER_CALL_PUSH_ORDER_UV));
            response.setOwnNer_pass_push_order_uv(record.get(STAT_WHOLE_PROCESS_INTER.OWNNER_PASS_PUSH_ORDER_UV));
            response.setTied_Card_call_uv(record.get(STAT_WHOLE_PROCESS_INTER.TIED_CARD_CALL_UV));
            response.setTied_Card_success_uv(record.get(STAT_WHOLE_PROCESS_INTER.TIED_CARD_SUCCESS_UV));
            response.setPreliminary_callback_uv(record.get(STAT_WHOLE_PROCESS_INTER.PRELIMINARY_CALLBACK_UV));
            response.setSign_call_uv(record.get(STAT_WHOLE_PROCESS_INTER.SIGN_CALL_UV));
            return response;
        });
    }
}
